<%
# Copyright (C) 2011 - present Instructure, Inc.
#
# This file is part of Canvas.
#
# Canvas is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, version 3 of the License.
#
# Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
%>

<% grading_standard ||= GradingStandard.default_instance %>

<div class="grading_standard" id="grading_standard_<%= grading_standard && grading_standard.id ? grading_standard.id : 'blank' %>">
  <div class="display_grading_standard">
    <div class="standard_title text-center">
      <% if !read_only %>
      <div style="float: right;" class="displaying links">
        <a href="#"
           class="find_grading_standard_link no-hover"
           aria-label="{{#t}}Find an Existing Grading Scheme{{/t}}"
           title="{{#t}}Find an Existing Grading Scheme{{/t}}">
          <%= image_tag "find.png", :alt => t('find', "Find") %> {{#t}}Select Another Scheme{{/t}}
        </a>
        <a href="#"
           title="{{#t}}Edit Grading Scheme{{/t}}"
           aria-label="{{#t}}Edit Grading Scheme{{/t}}"
           class="edit_grading_standard_link no-hover <%= 'read_only' if grading_standard && grading_standard.assessed_assignment? %>">
          {{> icons/edit}}
        </a>
        <a href="#"
           title="{{#t}}Remove Grading Scheme{{/t}}"
           aria-label="{{#t}}Remove Grading Scheme{{/t}}"
           class="remove_grading_standard_link no-hover <%= 'read_only' unless grading_standard %>">
         {{> icons/end}}
        </a>
      </div>
      <% end %>
      <strong class="displaying title"><%= grading_standard.title %></strong>
      <div class="editing_box">
        <label for="grading_standard_title"><%= before_label('scheme_name', 'Scheme Name') %></label>
        <input type="text"
               id="grading_standard_title"
               name="grading_standard[title]"
               class="scheme_name"
               value="<%= grading_standard.title %>"
               style="width: 200px;"/>
      </div>
    </div>
    <table style="width: 100%; margin-bottom: 20px;" class="grading_standard_data">
      <caption class="screenreader-only">
        {{#t}}Current grading scheme for this assignment{{/t}}
      </caption>
      <thead>
        <tr>
          <th id="name_header" style="text-align: left; padding-right: 10px; width: 25%;"><%= before_label('name', 'Name') %></th>
          <th colspan="3" style="width: 75%;">
            <div style="float: left; margin-left: 10px;"><%= before_label('range', 'Range') %></div>
            <div class="clear"></div>
          </th>
        </tr>
      </thead>
      <tr class="insert_grading_standard" style="display: none; font-size: 0.7em; line-height: 0.6em;">
        <td colspan="4">
          <a href="#" class="insert_grading_standard_link">{{#t}}insert here{{/t}}</a>
        </td>
      </tr>
      <% grading_standard.data.each_with_index do |row, idx| %>
        <tr class="grading_standard_row">
          <td style="width: 25%;">
            <div class="editing_box">
              <input type="text" class="standard_name" name="grading_standard[standard_data][scheme_<%= idx %>][name]" value="<%= row[0] %>" style="width: 100px;" aria-labelledby="name_header"/>
            </div>
            <div class="displaying name">
              <%= row[0] %>
            </div>
          </td>
          <td style="padding: 2px 5px; width: 25%;" class="max_score_cell" aria-label="{{#t}}Upper limit of range{{/t}}">
            <div class="editing_box" aria-hidden="true">
              <span class="edit_max_score">
                <% if idx == 0 %>
                  <%= I18n.n(100) %>
                <% else %>
                  <%= I18n.n((grading_standard.data[idx - 1][1] * 100).round(2)) %>
                <% end %>
              </span>%
            </div>
            <div class="displaying">
              <span class="max_score">
                <% if idx == 0 %>
                  <%= I18n.n(100) %>
                <% else %>
                  &lt; <%= I18n.n((grading_standard.data[idx - 1][1] * 100).round(2)) %>
                <% end %>
              </span>%
            </div>
          </td>
          <td style="padding: 2px 5px; width: 25%;">
            <div class="editing_box">
              <span style="padding-right: 5px;" aria-hidden="true">{{#t}}%{lower_number} to %{higher_number}', :lower_number => '', :higher_number => '{{/t}}</span>
              <input
                type="text"
                class="standard_value"
                title="{{#t}}Lower limit of range{{/t}}"
                aria-label="{{#t}}Lower limit of range{{/t}}"
                name="grading_standard[standard_data][scheme_<%= idx %>][value]"
                value="<%= I18n.n(row[1] * 100) %>"
                style="width: 37px;"/>%
            </div>
            <div class="displaying">
              <span style="padding-right: 5px;">{{#t}}%{lower_number} to %{higher_number}', :lower_number => '', :higher_number => '{{/t}}</span>
              <span class="min_score"><%= I18n.n((row[1] * 100).round(2)) %></span>%
            </div>
          </td>
          <td class="editing_box" style="padding: 2px 5px 2px 15px; width: 25%;">
            <a href="#" class="delete_row_link no-hover" tabindex="0" aria-label="{{#t}}Remove row')%>" title="<%= t('remove_row', 'Remove row{{/t}}">{{> icons/end}}</a>
          </td>
        </tr>
        <tr class="insert_grading_standard" style="display: none; font-size: 0.7em; line-height: 0.6em;">
          <td colspan="4">
            <a href="#" class="insert_grading_standard_link">{{#t}}insert here{{/t}}</a>
          </td>
        </tr>
      <% end %>
      <tr class="insert_grading_standard" style="display: none; font-size: 0.7em; line-height: 0.6em;">
        <td colspan="4">
          <a href="#" class="insert_grading_standard_link">{{#t}}insert here{{/t}}</a>
        </td>
      </tr>
      <tr class="grading_standard_row blank" style="display: none;">
        <td>
          <div class="editing_box">
            <input type="text" class="standard_name" name="grading_standard[standard_data][scheme_blank][name]" value="" style="width: 100px;"/>
          </div>
          <div class="displaying">
            &nbsp;
          </div>
        </td>
        <td style="padding: 2px 5px;">
          <div class="editing_box">
            <span style="padding-right: 5px;">{{#t}}%{lower_number} to %{higher_number}', :lower_number => '', :higher_number => '{{/t}}</span>
            <span class="edit_max_score">&nbsp;</span>%
          </div>
          <div class="displaying">
            <span style="padding-right: 5px;">{{#t}}%{lower_number} to %{higher_number}', :lower_number => '', :higher_number => '{{/t}}</span>
            <span class="max_score">&nbsp;</span>%
          </div>
        </td>
        <td style="padding: 2px 5px;">
          <div class="editing_box">
            <input type="text" class="standard_value" name="grading_standard[standard_data][scheme_blank][value]" value="" style="width: 30px;"/>%
          </div>
          <div class="displaying">
            <span class="min_score">&nbsp;</span>%
          </div>
        </td>
        <td class="editing_box" style="padding: 2px 5px 2px 15px;">
          <a href="#" class="delete_row_link no-hover">{{> icons/end}}</a>
        </td>
      </tr>
    </table>
    <div class="displaying" style="text-align: right;">
      <% if !read_only %>
        <a href="<%= context_url(@context, :context_grading_standards_url) %>">{{#t}}manage grading schemes{{/t}}</a>
      <% end %>
      <button type="button" class="done_button btn">{{#t}}Done{{/t}}</button>
    </div>
    <div class="editing_box form-actions">
      <button type="button" class="btn cancel_button">{{#t}}Cancel{{/t}}</button>
      <button type="button" class="btn btn-primary save_button">{{#t}}Save{{/t}}</button>
    </div>
  </div>
  <div style="display: none;" class="find_grading_standard">
    <a href="<%= context_url(@context, :context_grading_standards_url) %>" rel="nofollow" class="grading_standards_url" style="display: none;">&nbsp;</a>
    <div class="loading_message" style="margin: 10px; text-align: center;">
      {{#t}}Loading Grading Standards...{{/t}}
    </div>
    <% css_bundle("side_tabs_table") %>
    <table class="side_tabs_table grading_standards_holder" style="display: none;">
      <tr>
        <td class="left">
          <ul class="grading_standards_select unstyled_list side_tabs">
            <li class="grading_standard_select blank" style="display: none;">
              <a href="#">
                <span class="title">{{#t}}Some standard{{/t}}</span>
                <span class="id" style="display: none;">&nbsp;</span>
              </a>
              <div class="display_name" style="font-size: 0.8em;">{{#t}}Full name, smaller text{{/t}}</div>
            </li>
          </ul>
        </td>
        <td class="right">
          <div class="side_tabs_content grading_standards">
            <div style="margin-bottom: 10px; display: none;" class="grading_standard_brief blank">
              <span class="id" style="display: none;">&nbsp;</span>
              <div>
                <div style="float: left;">
                  <a href="#" class="select_grading_standard_link"><b class="title">&nbsp;</b></a>
                  <span class="user_name" style="padding-left: 10px; font-size: 0.8em; color: #888;">
                    &nbsp;
                  </span>
                </div>
                <div class="clear"></div>
              </div>
              <div class="clear"></div>
              <div style="margin-left: 30px;" class="details">
                <table style="font-size: 0.8em;">
                  <tr class="details_row blank" style="display: none;">
                    <td class="name" style="padding-right: 20px;">&nbsp;</td>
                    <td style="padding: 2px 5px;"><span class="value">&nbsp;</span>%</td>
                    <td style="padding: 2px 5px;">{{#t}}%{lower_number} to %{higher_number}', :lower_number => '', :higher_number => '{{/t}}</td>
                    <td style="padding: 2px 5px;"><span class="next_value">&nbsp;</span>%</td>
                  </tr>
                </table>
              </div>
              <button type="button" class="btn select_grading_standard_link">{{#t}}Use This Grading Standard{{/t}}</button>
            </div>
          </div>
        </td>
      </tr>
    </table>
    <div style="margin-top: 5px; text-align: right"><a href="#" class="cancel_find_grading_standard_link">{{#t}}Cancel{{/t}}</a></div>
  </div>
  <a href="<%= context_url(((grading_standard.context || @context) rescue @context), :context_grading_standard_url, grading_standard ? grading_standard.id : "{{ id }}") rescue "#" %>" style="display: none;" class="update_grading_standard_url">&nbsp;</a>
  <textarea id="default_grading_standard_data" style="display: none;"><%= GradingStandard.default_grading_standard.to_json %></textarea>
</div>
